T = 1e3;       % Sample size
t = (1:T)';    % Time multiple

rng(142857);   % For reproducibility

y1 = randn(T,1) + .2*t; % Trend stationary

Mdl2 = arima('D',1,'Constant',0.2,'Variance',1);
y2 = simulate(Mdl2,T,'Y0',0); % Difference stationary

Mdl3 = arima('AR',0.99,'Constant',0.2,'Variance',1);
y3 = simulate(Mdl3,T,'Y0',0); % AR(1)

Mdl4 = arima('D',1,'Constant',0.2,'Variance',1);
sigma = (sin(t/200) + 1.5)/2; % Std deviation
e = randn(T,1).*sigma;        % Innovations
y4 = filter(Mdl4,e,'Y0',0);   % Heteroscedastic

%%
y = [y1 y2 y3 y4];
plot1 = plot(y(1:100,:));
set(plot1(1),'LineWidth',2)
set(plot1(3),'LineStyle',':','LineWidth',2)
set(plot1(4),'LineStyle',':','LineWidth',2)
title('{\bf First 100 Periods of Each Series}')
legend('Trend Stationary','Difference Stationary','AR(1)',...
   'Heteroscedastic','location','northwest')

%%
plot2 = plot(y);
set(plot2(1),'LineWidth',2)
set(plot2(3),'LineStyle',':','LineWidth',2)
set(plot2(4),'LineStyle',':','LineWidth',2)
title('{\bf Each Entire Series}')
legend('Trend Stationary','Difference Stationary','AR(1)',...
   'Heteroscedastic','location','northwest')

%%
hY1 = adftest(y1, 'model','ts', 'lags',2)
hY2 = adftest(y2, 'model','ts', 'lags',2)
hY4 = adftest(y4, 'model','ts', 'lags',2)

%%
hY1= kpsstest(y1, 'lags',2, 'trend',true)
hY2 = kpsstest(y2, 'lags',2, 'trend',true)
hY3 = kpsstest(y3, 'lags',2)
hY4 = kpsstest(y4, 'lags',2, 'trend',true)